#include "data.h"
#include "bsp_iic.h"
#include "string.h"
#include "stdio.h"

uint16_t g_ch_rev[8];//通道反向标志
int16_t g_ch_offset[8];   //通道中心点偏移值
uint16_t g_ch_end_point_p[8]; //通道最大值
uint16_t g_ch_end_point_n[8]; //通道最小值

void parameter_set(uint16_t addr, uint16_t value)
{
    uint8_t buf[2];
    
    buf[0] = value>>8;
    buf[1] = value;
    
    eeprom_write(addr, buf, 2);
}

uint16_t parameter_get(uint16_t addr)
{
    uint8_t buf[2];
    uint16_t value;
    
    eeprom_read(addr, buf, 2);
    value = (buf[0]<<8) | buf[1];
    
    return value;
}

void parameter_load(uint8_t init_flag)
{
	uint8_t init_value;
	
	init_value = parameter_get(FLASH_ADDR_INIT_FLAG);
    /* 设置初始值 */
	if(init_value != init_flag)
	{          
        parameter_set(FLASH_ADDR_CH_REV_1, 0);
        parameter_set(FLASH_ADDR_CH_REV_2, 0);
        parameter_set(FLASH_ADDR_CH_REV_3, 0);
        parameter_set(FLASH_ADDR_CH_REV_4, 0);
        parameter_set(FLASH_ADDR_CH_REV_5, 0);
        parameter_set(FLASH_ADDR_CH_REV_6, 0);
        parameter_set(FLASH_ADDR_CH_REV_7, 0);
        parameter_set(FLASH_ADDR_CH_REV_8, 0);
        
        parameter_set(FLASH_ADDR_CH_OFFSET_1, 0);
        parameter_set(FLASH_ADDR_CH_OFFSET_2, 0);
        parameter_set(FLASH_ADDR_CH_OFFSET_3, 0);
        parameter_set(FLASH_ADDR_CH_OFFSET_4, 0);
        parameter_set(FLASH_ADDR_CH_OFFSET_5, 0);
        parameter_set(FLASH_ADDR_CH_OFFSET_6, 0);
        parameter_set(FLASH_ADDR_CH_OFFSET_7, 0);
        parameter_set(FLASH_ADDR_CH_OFFSET_8, 0);
        
        
        parameter_set(FLASH_ADDR_CH_END_POINT_P1, 2000);
        parameter_set(FLASH_ADDR_CH_END_POINT_P2, 2000);
        parameter_set(FLASH_ADDR_CH_END_POINT_P3, 2000);
        parameter_set(FLASH_ADDR_CH_END_POINT_P4, 2000);
        parameter_set(FLASH_ADDR_CH_END_POINT_P5, 2000);
        parameter_set(FLASH_ADDR_CH_END_POINT_P6, 2000);
        parameter_set(FLASH_ADDR_CH_END_POINT_P7, 2000);
        parameter_set(FLASH_ADDR_CH_END_POINT_P8, 2000);
        
        parameter_set(FLASH_ADDR_CH_END_POINT_N1, 1000);
        parameter_set(FLASH_ADDR_CH_END_POINT_N2, 1000);
        parameter_set(FLASH_ADDR_CH_END_POINT_N3, 1000);
        parameter_set(FLASH_ADDR_CH_END_POINT_N4, 1000);
        parameter_set(FLASH_ADDR_CH_END_POINT_N5, 1000);
        parameter_set(FLASH_ADDR_CH_END_POINT_N6, 1000);
        parameter_set(FLASH_ADDR_CH_END_POINT_N7, 1000);
        parameter_set(FLASH_ADDR_CH_END_POINT_N8, 1000);
        
        parameter_set(FLASH_ADDR_INIT_FLAG, init_flag);
	}
    		
	g_ch_rev[0] = parameter_get(FLASH_ADDR_CH_REV_1);
    g_ch_rev[1] = parameter_get(FLASH_ADDR_CH_REV_2);
    g_ch_rev[2] = parameter_get(FLASH_ADDR_CH_REV_3);
    g_ch_rev[3] = parameter_get(FLASH_ADDR_CH_REV_4);
    g_ch_rev[4] = parameter_get(FLASH_ADDR_CH_REV_5);
    g_ch_rev[5] = parameter_get(FLASH_ADDR_CH_REV_6);
    g_ch_rev[6] = parameter_get(FLASH_ADDR_CH_REV_7);
    g_ch_rev[7] = parameter_get(FLASH_ADDR_CH_REV_8);
    
    g_ch_offset[0] = parameter_get(FLASH_ADDR_CH_OFFSET_1);
    g_ch_offset[1] = parameter_get(FLASH_ADDR_CH_OFFSET_2);
    g_ch_offset[2] = parameter_get(FLASH_ADDR_CH_OFFSET_3);
    g_ch_offset[3] = parameter_get(FLASH_ADDR_CH_OFFSET_4);
    g_ch_offset[4] = parameter_get(FLASH_ADDR_CH_OFFSET_5);
    g_ch_offset[5] = parameter_get(FLASH_ADDR_CH_OFFSET_6);
    g_ch_offset[6] = parameter_get(FLASH_ADDR_CH_OFFSET_7);
    g_ch_offset[7] = parameter_get(FLASH_ADDR_CH_OFFSET_8);
    
    g_ch_end_point_p[0] = parameter_get(FLASH_ADDR_CH_END_POINT_P1);
    g_ch_end_point_p[1] = parameter_get(FLASH_ADDR_CH_END_POINT_P2);
    g_ch_end_point_p[2] = parameter_get(FLASH_ADDR_CH_END_POINT_P3);
    g_ch_end_point_p[3] = parameter_get(FLASH_ADDR_CH_END_POINT_P4);
    g_ch_end_point_p[4] = parameter_get(FLASH_ADDR_CH_END_POINT_P5);
    g_ch_end_point_p[5] = parameter_get(FLASH_ADDR_CH_END_POINT_P6);
    g_ch_end_point_p[6] = parameter_get(FLASH_ADDR_CH_END_POINT_P7);
    g_ch_end_point_p[7] = parameter_get(FLASH_ADDR_CH_END_POINT_P8);
    
    g_ch_end_point_n[0] = parameter_get(FLASH_ADDR_CH_END_POINT_N1);
    g_ch_end_point_n[1] = parameter_get(FLASH_ADDR_CH_END_POINT_N2);
    g_ch_end_point_n[2] = parameter_get(FLASH_ADDR_CH_END_POINT_N3);
    g_ch_end_point_n[3] = parameter_get(FLASH_ADDR_CH_END_POINT_N4);
    g_ch_end_point_n[4] = parameter_get(FLASH_ADDR_CH_END_POINT_N5);
    g_ch_end_point_n[5] = parameter_get(FLASH_ADDR_CH_END_POINT_N6);
    g_ch_end_point_n[6] = parameter_get(FLASH_ADDR_CH_END_POINT_N7);
    g_ch_end_point_n[7] = parameter_get(FLASH_ADDR_CH_END_POINT_N8);
}



